# -*- python -*-
# Copyright (C) 2007, Levin Du.
# Author:  Levin Du <zsdjw@21cn.com>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
import glob

Import('env', 'mi_config', 'MbPkgMaker')

class BusyboxMaker(MbPkgMaker):
    alias = 'mbbusybox'
    name = 'busybox'
    version = mi_config.busybox_version
    source_list = ['${package}.tar.bz2']
    patch_list = ['${package}-mb.patch']
    target_list = ['libapplets.a',
                   'libbb/lib.a',
                   'archival/libunarchive/lib.a',
                   'coreutils/libcoreutils/lib.a']
    install_map = {'lib/': ['libapplets.a'],
                    'lib/libbb.a': ['libbb/lib.a'],
                    'lib/libunarchive.a': ['archival/libunarchive/lib.a'],
                    'lib/libcoreutils.a': ['coreutils/libcoreutils/lib.a'],
                    }

    def configure(self):
        env = self.env
        env.Command('$build_prefix/.configured',
                    ['${name}-${version}.config',
                     '$build_prefix/.prepared'],
                    ['cp $SOURCE $build_prefix/.config',
                     'make -C $build_prefix oldconfig',
                     self.get_touch_cmd()])

    def build(self):
        env = self.env
        applets = [#'dir/applet'
                   #v1.4.1 'shell/cmdedit',          
                  'shell/lash',
                  'archival/cpio',
                  'archival/bbunzip', #v1.4.1 'archival/gunzip',
                  'networking/interface',
                  'networking/ifconfig',
                  'networking/route',
                  'networking/wget',
                  'init/init',
                  #v1.4.1 'init/init_shared',
                  'modutils/insmod',
                  'modutils/modprobe',
                  'modutils/rmmod',
                  'util-linux/mount',
                  'util-linux/umount',
                  'util-linux/mdev',
                  'procps/ps',
                  'applets/applets',
                  'coreutils/ls',
                  'coreutils/cat',
                  'coreutils/mv',
                  'coreutils/test',      #required by lash
                  'coreutils/true',      #required by lash
                  'coreutils/false',     #required by lash
                  ]

        # applet.c -> applet.o
        applet_srcs = ['$build_prefix/%s.c' % applet for applet in applets]
        applet_objs = ['$build_prefix/%s.o' % applet for applet in applets]
        env.SideEffect(applet_srcs,
                       '$build_prefix/.configured')
        env.Depends(applet_objs,
                    '$build_prefix/.configured')
        env.Command(applet_objs,
                    applet_srcs,
                    'make CROSS_COMPILE=$CROSS_COMPILE ' +
                    'KBUILD_VERBOSE=1 -C $build_prefix ' +
                    ' '.join(['%s.o' % applet for applet in applets]))

        # build libapplets.a
        env.StaticLibrary(target = '$build_prefix/libapplets.a',
                          source = applet_objs)

        # -> libbb.a, libunarchive.a
        for lib in ['libbb',
                    'archival/libunarchive',
                    'coreutils/libcoreutils']:
            env.Command('$build_prefix/$lib/lib.a',
                        '$build_prefix/.configured',
                        ['make CROSS_COMPILE=$CROSS_COMPILE KBUILD_VERBOSE=1 -C $build_prefix $lib'],
                        lib=lib)

pkg = BusyboxMaker(env)
pkg.make()
Alias('mblib', pkg.alias)
